package com.crape.account.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.crape.account.bo.IAccountBO;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.Article;
import com.crape.common.model.Question;
import com.crape.common.model.Subject;
import com.crape.common.thread.ThreadObjManager;
import com.crape.common.utils.PageUtils;
import com.crape.faq.bo.IQuestionBO;
import com.crape.topic.bo.IArticleBO;
import com.crape.topic.bo.ISubjectBO;

@Controller
public class UserCenterController {

	private IAccountBO accountBOImpl;
	private ISubjectBO subjectBOImpl;
	private IQuestionBO questionBOImpl;
	private IArticleBO articleBOImpl;

	@Autowired
	public void setAccountBOImpl(IAccountBO accountBOImpl) {
		this.accountBOImpl = accountBOImpl;
	}

	@Autowired
	public void setSubjectBOImpl(ISubjectBO subjectBOImpl) {
		this.subjectBOImpl = subjectBOImpl;
	}

	@Autowired
	public void setQuestionBOImpl(IQuestionBO questionBOImpl) {
		this.questionBOImpl = questionBOImpl;
	}

	@Autowired
	public void setarticleBOImpl(IArticleBO articleBOImpl) {
		this.articleBOImpl = articleBOImpl;
	}

	@RequestMapping("/user/index.jsp")
	public String index(ModelMap modelMap, Long accountid) {
		return this.articlesIPublished(modelMap, accountid, null);
	}

	@RequestMapping("/user/articlesIPublished.jsp")
	public String articlesIPublished(ModelMap modelMap, Long accountid, Integer pageNo) {
		accountid = this.getAccountCommons(accountid, modelMap);
		if (accountid == null) {
			modelMap.put("message", "对不起，尚未登陆！");
			return "/alert.html";
		}
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Long count = articleBOImpl.countArticlesByAccountid(accountid);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<Article> Articles = articleBOImpl.getArticlesByAccountid(accountid, startIndex, pageSize);
			modelMap.put("articles", Articles);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/user/articlesIPublished.html";
	}

	@RequestMapping("/user/articlesICommented.jsp")
	public String articlesICommented(ModelMap modelMap, Long accountid, Integer pageNo) {
		accountid = this.getAccountCommons(accountid, modelMap);
		if (accountid == null) {
			modelMap.put("message", "对不起，尚未登陆！");
			return "/alert.html";
		}
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Long count = articleBOImpl.countArticleCommentsByAccountid(accountid);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<Article> Articles = articleBOImpl.getCommentArticlesByAccountid(accountid, startIndex, pageSize);
			modelMap.put("articles", Articles);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/user/articlesICommented.html";
	}

	@RequestMapping("/user/subjectsIFollowed.jsp")
	public String subjects(ModelMap modelMap, Long accountid, Integer pageNo) {
		accountid = this.getAccountCommons(accountid, modelMap);
		if (accountid == null) {
			modelMap.put("message", "对不起，尚未登陆！");
			return "/alert.html";
		}
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Long count = subjectBOImpl.countSubjectAttentionsByAccountid(accountid);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<Subject> attentionSubjects = subjectBOImpl.getSubjectAttentionsByAccountid(accountid, startIndex, pageSize);
			modelMap.put("attentionSubjects", attentionSubjects);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/user/subjectsIFollowed.html";
	}

	@RequestMapping("/user/hobbies.jsp")
	public String hobbies(ModelMap modelMap, Long accountid, Integer pageNo) {
		accountid = this.getAccountCommons(accountid, modelMap);
		if (accountid == null) {
			modelMap.put("message", "对不起，尚未登陆！");
			return "/alert.html";
		}
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Long count = accountBOImpl.countAllAttentions(accountid);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<AccountInfo> accounts = accountBOImpl.getUserAttentionsByAccountid(accountid, startIndex, pageSize);
			modelMap.put("accounts", accounts);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/user/hobbies.html";
	}

	@RequestMapping("/user/fans.jsp")
	public String fans(ModelMap modelMap, Long accountid, Integer pageNo) {
		accountid = this.getAccountCommons(accountid, modelMap);
		if (accountid == null) {
			modelMap.put("message", "对不起，尚未登陆！");
			return "/alert.html";
		}
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Long count = accountBOImpl.countAllUserFans(accountid);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<AccountInfo> accounts = accountBOImpl.getUserFansByAccountid(accountid, startIndex, pageSize);
			modelMap.put("accounts", accounts);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/user/fans.html";
	}

	@RequestMapping("/user/visitors.jsp")
	public String visitors(ModelMap modelMap, Long accountid, Integer pageNo) {
		accountid = this.getAccountCommons(accountid, modelMap);
		if (accountid == null) {
			modelMap.put("message", "对不起，尚未登陆！");
			return "/alert.html";
		}
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Long count = accountBOImpl.countAllUserVisitors(accountid);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<AccountInfo> accounts = accountBOImpl.getAllUserVisitors(accountid, startIndex, pageSize);
			modelMap.put("accounts", accounts);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/user/visitors.html";
	}

	@RequestMapping("/user/questionsIPublished.jsp")
	public String questionsIPublished(ModelMap modelMap, Long accountid, Integer pageNo) {
		accountid = this.getAccountCommons(accountid, modelMap);
		if (accountid == null) {
			modelMap.put("message", "对不起，尚未登陆！");
			return "/alert.html";
		}
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Long count = questionBOImpl.countPageQuestionsByAccountid(accountid);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<Question> questions = questionBOImpl.getPagedQuestionsByAccountid(accountid, startIndex, pageSize);
			modelMap.put("questions", questions);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/user/questionsIPublished.html";
	}

	@RequestMapping("/user/questionsIAnswered.jsp")
	public String questionsIAnswered(ModelMap modelMap, Long accountid, Integer pageNo) {
		accountid = this.getAccountCommons(accountid, modelMap);
		if (accountid == null) {
			modelMap.put("message", "对不起，尚未登陆！");
			return "/alert.html";
		}
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Long count = questionBOImpl.countAnswerQuestionsByAccountid(accountid);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<Question> questions = questionBOImpl.getPagedAnswerQuestionsByAccountid(accountid, startIndex, pageSize);
			modelMap.put("questions", questions);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/user/questionsIAnswered.html";
	}

	@RequestMapping("/user/questionsIFollowed.jsp")
	public String questionsIFollowed(ModelMap modelMap, Long accountid, Integer pageNo) {
		accountid = this.getAccountCommons(accountid, modelMap);
		if (accountid == null) {
			modelMap.put("message", "对不起，尚未登陆！");
			return "/alert.html";
		}
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Long count = questionBOImpl.countFollowedQuestionsByAccountid(accountid);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<Question> questions = questionBOImpl.getPagedFollowedQuestionsByAccountid(accountid, startIndex, pageSize);
			modelMap.put("questions", questions);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/user/questionsIFollowed.html";
	}

	private Long getAccountCommons(Long accountid, ModelMap modelMap) {
		if (accountid == null) {
			modelMap.put("viewUser", "self");
			accountid = ThreadObjManager.getLoginUser().getAccountid();
		} else {
			Long visitorid = ThreadObjManager.getLoginUser().getAccountid();
			accountBOImpl.addAcctVisitor(visitorid, accountid);
			modelMap.put("viewUser", "other");
		}
		AccountInfo account = accountBOImpl.getAccountInfoById(accountid);
		modelMap.put("account", account);
		return accountid;
	}
}
